import utime, os, sys
sys.path.append('./sdcard/')
from CameraStreamer_v1_1 import CameraStreamer
from media.sensor import *
from media.display import *
from media.media import *

def main_camera_stream():
    sensor = Sensor(id=2)
    sensor.reset()
    i = CameraStreamer(ip = "192.168.137.1", port = 6001, ssid = "Ciallo～(∠・ω< )⌒☆", pwd = "0d000721", w = 1280, h = 720, sensor=sensor);
    # 设置chn1输出大小为320x240
    sensor.set_framesize(width=800, height=448, chn=CAM_CHN_ID_1)
    # 设置chn1输出格式
    sensor.set_pixformat(Sensor.RGB888, chn=CAM_CHN_ID_1)

    Display.init(Display.ST7701, width=800, height=480, osd_num=2, to_ide=False)
    MediaManager.init()
    sensor.run()
    i.start()

    imgHUD = image.Image(800, 32, image.ARGB8888)

    clock = utime.clock()
    try:
        while 1:
            clock.tick()
            i.check()
            img = sensor.snapshot(chn=CAM_CHN_ID_1)
            Display.show_image(img, x=0, y=32, layer=Display.LAYER_OSD0)
            imgHUD.clear()
            imgHUD.draw_string_advanced(680, 0, 24, f"fps {clock.fps():2.1f}", color=(255, 0, 255))
            Display.show_image(imgHUD, x=0, y=0, layer=Display.LAYER_OSD1)
            os.exitpoint()
    except Exception as e:
        print(f"主循环异常：{e}")
    finally:
        sensor.stop()
        Display.deinit()
        i.deint()
        time.sleep_ms(100)
        MediaManager.deinit()

if __name__ == "__main__":
    main_camera_stream()
